package com.yl.wzy.banner;

import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.maning.mnvideoplayerlibrary.player.YLVideoPlayer;

import java.util.ArrayList;
import java.util.List;

/**
 * banner 适配器
 *
 * @author wzy
 */

public class BannerViewAdapter extends PagerAdapter {

    private List<View> listBean;

    BannerViewAdapter(List<View> list) {
        if (list == null) {
            list = new ArrayList<>();
        }
        this.listBean = list;
    }

    void setDataList(List<View> list) {
        if (list != null && list.size() > 0) {
            this.listBean = list;
        }
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        View view = listBean.get(position);

        container.addView(view);
        return view;
    }

   /* @Override
    public int getItemPosition(@NonNull Object o+3bject) {
        return POSITION_NONE;
    }*/

    @Override
    public int getCount() {
        return listBean.size();
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {

        View view = (View) object;
        if (view instanceof FrameLayout)  //  暂停播放
        {
            try {
                if (((FrameLayout) view).getChildCount() < 1) return;

                YLVideoPlayer videoPlayer = (YLVideoPlayer) ((FrameLayout) view).getChildAt(0);
                videoPlayer.pauseVideo();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        container.removeView(view);
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

}